/* -----------------
* brief
* 1. this is code gen by tools
 */

package service

import (
	"errors"
	"github.com/mjiulee/lego"
	"saas_sys_base/common"
	configService "saas_sys_base/modules/config/service"
	"saas_sys_base/sysconfig"

	"github.com/chanxuehong/wechat/mp/oauth2"
	// "github.com/chanxuehong/wechat/mp/base"
	"github.com/chanxuehong/wechat/mp/core"
	"sync"
)
import . "saas_sys_base/modules/wechat/models"

var gOnce sync.Once
var (
	gAccessTokenServer core.AccessTokenServer //= core.NewDefaultAccessTokenServer(wxAppId, wxAppSecret, nil)
	gWechatClient      *core.Client           //= core.NewClient(gAccessTokenServer, nil)
)

/*
var gWechatCliMap *common.WhoMap
func init() {
	gWechatCliMap =  common.NewWhoMap()
}
*/
type WechatService struct {
	// add your model here
	tbWechat         Wechat
	wechatSdkClient  WechatSdkClient
	WechatCliDefault *core.Client
	//WechatCliMap map[int64]*core.Client
	//value的实际类型为*core.Client
	//configService configService.ConfigService
	appconfigService configService.AppConfigService
}

func NewWechatService() *WechatService {
	wechatSvc := WechatService{}
	wechatSvc.WechatCliDefault = wechatSvc.GetMpClientDefault()

	return &wechatSvc
}

func NewWechatServiceByWho(who common.Who) *WechatService {
	wechatSvc := WechatService{}
	//wechatSvc.WechatCli = wechatSvc.GetMpClient(who)

	return &wechatSvc
}

func (t *WechatService) ListWechat(keyword string, page, psize int) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_wechat", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join":   []map[string]string{
			// map[string]string{"table": "tb_xx as t2", "on": "t2.xid = t1.id"},
		},
		"where": []lego.QMapWhereField{
			//{"t1.if_del", "=", 0, "AND", false},
		},
		"search": map[string]string{
			//"t1.id": req.Keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}
	return lego.QueryListByMap(queryMap)
}

/* 根据id获取记录
 * -----------------
 */
func (t *WechatService) GetWechatById(id int64) *Wechat {
	return t.tbWechat.RecodeById(id)
}

/* 添加
 * -----------------
 */
func (t *WechatService) SaveWechat(item2add *Wechat) bool {
	return t.tbWechat.AddRecode(item2add)
}

/* 删除(硬删除)*/
func (t *WechatService) DelWechatById(id int64) bool {
	return t.tbWechat.DelRecodeById(id)
}

/* 更新
 * -----------------*/
func (t *WechatService) UpdateWechat(rc *Wechat) bool {
	return t.tbWechat.UpdateRecode(rc)
}

/*自定义服务函数*/
/*小程序Endpoint*/
func (t *WechatService) GetEndpointDefault() *oauth2.Endpoint {
	wxAppId := sysconfig.Config.Wechat.WX_APPID
	wxAppSecret := sysconfig.Config.Wechat.WX_APPSECRET
	//wxrsp, err := wxHelper.GetSessionKey(appid, appsecret, code)

	mp := oauth2.NewEndpoint(wxAppId, wxAppSecret)
	return mp
}

// 获取公众号api client
func (t *WechatService) GetMpClientDefault() *core.Client {
	wxAppId := sysconfig.Config.Wechat.WX_MP_APPID
	wxAppSecret := sysconfig.Config.Wechat.WX_MP_APPSECRET
	//wxrsp, err := wxHelper.GetSessionKey(appid, appsecret, code)
	//if gAccessTokenServer == nil {
	gOnce.Do(func() {
		gAccessTokenServer = core.NewDefaultAccessTokenServer(wxAppId, wxAppSecret, nil)
		gWechatClient = core.NewClient(gAccessTokenServer, nil)
	})
	//}

	return gWechatClient
}

// 获取公众号api client
func (t *WechatService) GetMpClientByWho(who common.Who) (*core.Client, error) {
	//单例
	cli, err := t.wechatSdkClient.GetMpClientByWho(who)
	if err != nil {
		return nil, err
	}

	// TODO config, err := t.configService.GetConfigByWho(who)

	return cli, nil
}

//通过weappid获取公众号或小程序的client（不包括商户client）
func (t *WechatService) GetClientByWeappid(weappid string) (*core.Client, error) {
	cli := t.wechatSdkClient.GetWxClientByWeappid(weappid)
	if cli == nil {
		return nil, errors.New("未找到appid")
	}

	// TODO config, err := t.configService.GetConfigByWho(who)

	return cli, nil
}




func (t *WechatService) GetEndpointFromConfig(who common.Who) (*oauth2.Endpoint, string) {
	config, err := t.appconfigService.GetConfigByWho(who)
	if err != nil {
		return nil, ""
	}

	wxAppId := config.App_Config_Data.Wechat.WX_APPID
	wxAppSecret := config.App_Config_Data.Wechat.WX_APPSECRET
	//wxrsp, err := wxHelper.GetSessionKey(appid, appsecret, code)

	mp := oauth2.NewEndpoint(wxAppId, wxAppSecret)
	return mp, config.App_Config_Data.Wechat.WX_APPID
}
